/*  -------------------------- (C) COPYRIGHT 2020 Fortiortech ShenZhen ---------------------------*/
/*  File Name      : Interrupt.c
/*  Author         : Fortiortech  Appliction Team
/*  Version        : V1.0
/*  Date           : 2020-03-24
/*  Description    : This file contains XX-XX-XX function used for Motor Control.
/*  ----------------------------------------------------------------------------------------------*/
/*                                     All Rights Reserved
/*  ----------------------------------------------------------------------------------------------*/

#include "FU68xx_4.h"
#include <MyProject.h>

#if defined (SPI_DBG_SW)
uint16 xdata spidebug[4] = { 0 };
#endif
/* Private variables ----------------------------------------------------------------------------*/
uint8 UARTData = 0;		// 定义一个用于接收UART数据的变量

//LVW & TSD interrupt
void LVW_TSD_INT(void) interrupt 0                                          
{
    if (ReadBit(LVSR, LVWIF))
    {
        if (ReadBit(LVSR, LVWF))
        {
            RST_SR |= 0x01;
            ClrBit(LVSR, LVWF);
        }
        
        ClrBit(LVSR, LVWIF);
    }
    
    if (TSDIF)
    {
        TSDIF = 0;
    }
}

/*-------------------------------------------------------------------------------------------------
    Function Name : void FO_INT(void)
    Description   : FO_INT interrupt，硬件FO过流保护，关断输出，中断优先级最高
    Input         : 无
    Output        : 无
-------------------------------------------------------------------------------------------------*/
void FO_INT(void) interrupt 1                                                 // 硬件FO过流中断，关闭输出
{
    if (IF0)
    {
        FaultProcess();                                                       // 关闭输出
        mcFaultSource = FaultHardOVCurrent;                                   // 硬件过流保护
        mcState = mcFault;                                                    // 状态为mcFault
        IF0 = 0;                                                              // clear P00 interrupt flag
    }
}

/*---------------------------------------------------------------------------*/
/* Name     :   void EXTERN_INT(void) interrupt 2
/* Input    :   NO
/* Output   :   NO
/* Description: 睡眠唤醒
/*---------------------------------------------------------------------------*/
void EXTERN_INT(void) interrupt 2
{
    ;
}

/*---------------------------------------------------------------------------*/
/* Name     :   void DRV_ISR(void) interrupt 3
/* Input    :   NO
/* Output   :   NO
/* Description: FOC中断(Drv中断),每个载波周期执行一次，用于处理响应较高的程序，中断优先级第二。DCEN开了就会产生中断。
/*---------------------------------------------------------------------------*/
void DRV_ISR(void) interrupt 3
{
    /* 软件过流保护 */
    if (ReadBit(DRV_SR, FGIF))
    {
        ClrBit(DRV_SR, FGIF);
    }
    /* 比较中断 */
    if (ReadBit(DRV_SR, DCIF))                                                  
    {
        if (mcState == mcPosiCheck)
        {
            SetBit(ADC_CR, ADCBSY); //使能ADC的DCBUS采样
            RPD_Get_ID();
        }
        Fault_Overcurrent(&mcCurVarible);
        #if ((FRDetectMethod == FOCMethod)&&(TailWind_Mode == TailWind))
        TailWindSpeedDetect();                                                  //顺逆风检测
        #endif
        #if defined (SPI_DBG_SW)                                                //软件调试模式
        spidebug[0] = SOFT_SPIDATA0;
        spidebug[1] = SOFT_SPIDATA1;
        spidebug[2] = SOFT_SPIDATA2;
        spidebug[3] = SOFT_SPIDATA3;
        #endif
        ClrBit(DRV_SR, DCIF);
    }
}

/*---------------------------------------------------------------------------*/
/* Name     :   void TIM2_INT(void) interrupt 4
/* Input    :   NO
/* Output   :   NO
/* Description: 正反转检测(ISD)
/*---------------------------------------------------------------------------*/
void TIM2_INT(void) interrupt 4
{
    if (ReadBit(TIM2_CR1, T2IR))
    {
        ClrBit(TIM2_CR1, T2IR);
    }
    
    if (ReadBit(TIM2_CR1, T2IP))
    {
        #if (FRDetectMethod == RSDMethod)
        RSDFRDetect();      //ISD正反转检测
        #endif
        ClrBit(TIM2_CR1, T2IP);
    }
    
    if (ReadBit(TIM2_CR1, T2IF)) /* 溢出中断,用于判断静止,时间为349ms */
    {
        #if (FRDetectMethod == RSDMethod)
        RSDDetect.RSDState = Static;
        RSDDetect.RSDFlag  = 1;
        #endif
        ClrBit(TIM2_CR1, T2IF);
    }
}

/*-------------------------------------------------------------------------------------------------
    Function Name : void CMP3_INT(void)
    Description   : CMP3：硬件比较器过流保护，关断输出，中断优先级最高
                                                                                                                                                                                                       CMP0/1/2：顺逆风判断
    Input         : 无
    Output        : 无
-------------------------------------------------------------------------------------------------*/
void CMP3_INT(void)  interrupt 12
{
    if (ReadBit(CMP_SR, CMP3IF))
    {
        if (mcState != mcPosiCheck)
        {
            FaultProcess();                                                        // 关闭输出
            mcFaultSource = FaultHardOVCurrent;                                    // 硬件过流保护
            mcState       = mcFault;                                               // 状态为mcFault
        }
        
        ClrBit(CMP_SR, CMP3IF);
    }
}

/*---------------------------------------------------------------------------*/
/* Name     :   void TIM23_INT(void) interrupt 9
/* Input    :   NO
/* Output   :   NO
/* Description: Capture PWM ，中断优先级第二，高于FOC中断，用于PWM调速
/*---------------------------------------------------------------------------*/
void TIM3_INT(void) interrupt 9
{
    if (ReadBit(TIM3_CR1, T3IR))
    {
        ClrBit(TIM3_CR1, T3IR);
    }
    
    /* 周期中断 */
    if (ReadBit(TIM3_CR1, T3IP))
    {
        /* 遥控处理 */
        //IRValue();
        ClrBit(TIM3_CR1, T3IP);
    }
    
    if (ReadBit(TIM3_CR1, T3IF))
    {
        ClrBit(TIM3_CR1, T3EN);
        TIM3__ARR = 0; // TIM3 Period = 100us
        TIM3__DR = 0;
        TIM3__CNTR = 0;
        IRScan.BitCnt = 0;
        IRScan.CurrentDataCode = 0;
        IRScan.OverFlowStatus = 0;
        ClrBit(TIM3_CR1, T3IF);
        SetBit(TIM3_CR1, T3EN);
    }
}


/*---------------------------------------------------------------------------*/
/* Name     :   void TIM4S_INT(void) interrupt 10
/* Input    :   NO
/* Output   :   NO
/* Description: 1.1ms定时器中断(SYS TICK中断)
                2.用于处理附加功能
                3.如控制环路响应、各种保护等
                4.中断优先级低于FO中断和FOC中断
/*---------------------------------------------------------------------------*/
void SYStick_INT(void) interrupt 10
{
    /* SYS TICK中断 */
    if (ReadBit(DRV_SR, SYSTIF))
    {
        /* 使能ADC的DCBUS采样 */
        SetBit(ADC_CR, ADCBSY); 

        if (mcState == mcRun)
        {
            /* 功率滤波 */
            mcFocCtrl.CurrentPower = FOC__POW << 1;
            LPF_MDU(mcFocCtrl.CurrentPower, 10, mcFocCtrl.Powerlpf, mcFocCtrl.Powerlpf_LSB);
        }
        else
        {
            mcFocCtrl.Powerlpf = 0;
        }
        
        if ((mcState != mcInit) && (mcState != mcReady))
        {
            /* 速度滤波、反电动势滤波 */
            LPF_MDU(FOC__EOME, 10, mcFocCtrl.SpeedFlt, mcFocCtrl.SpeedFlt_LSB);
        }
        else
        {
            mcFocCtrl.SpeedFlt = 0;
        }

        Atan_Us_MDU(FOC__EALP, FOC__EBET, mcFocCtrl.EsValue);
        /* 环路响应，如速度环、转矩环、功率环等 */
        Speed_response();                                                      
        #if (StartONOFF_Enable)
        motoronoff();
        #endif

        /* DCbus的采样获取值并滤波 */
        AdcSampleValue.ADCDcbus = ADC14_DR; //FOC_UDCFLT
        LPF_MDU((AdcSampleValue.ADCDcbus), 10, mcFocCtrl.mcDcbusFlt, mcFocCtrl.mcDcbusFlt_LSB);
        mcFocCtrl.mcDcbusFlt    = AdcSampleValue.ADCDcbus;

        /* 故障保护函数功能，如过欠压保护、启动保护、缺相、堵转等 */
        Fault_Detection();
        Fault_Buzzer();
		LED_Display();
        
        //IRScanControl();
        //IRONOFF_Control();
        //AutoPowerControl();
        CurrentZeroDetect();

        /*****电机状态机的时序处理*****/
        if (mcFocCtrl.State_Count > 0)
        {
            mcFocCtrl.State_Count--;
        }
        
        if (Time.PowerUpTimeCount < 60000)
        {
            Time.PowerUpTimeCount++;    //上电一分钟计时
        }
        
        if (Time.BuzzTimer < 17000)
        {
            Time.BuzzTimer++;
        }
        
        if (mcState == mcRun)
        {
            if (Time.mcRunStateCount < 60000)	//Run状态计时
            {
                Time.mcRunStateCount++;
            }
        }
        else
        {
            Time.mcRunStateCount = 0;
        }
        
        if (mcState == mcRun)
        {
            if (POWER_LIMIT_ENALBE)
            {
                if (mcFocCtrl.Powerlpf > 3000 )
                {
                    if (mcFocCtrl.mcIqref > SOUTMIN)
                    {
                        mcFocCtrl.mcIqref -= 2;
                        PI3_UKH = mcFocCtrl.mcIqref;
                        FOC_IQREF = mcFocCtrl.mcIqref;
                    }
                }
            }
        }
        
        #if (FRDetectMethod == FOCMethod)
        FOCTailWindTimeLimit();
        #endif
        
        if (!ReadBit(TIM4_CR1, T4EN))
        {
            GP10 = 0;
        }
        
        /*****电机启动爬坡函数处理*****/
        StarRampDealwith();
        ClrBit(DRV_SR, SYSTIF);                                                  // 清零标志位

    }
}

void INT11(void) interrupt 11
{
    if (ReadBit(TIM4_CR1, T4IF))
    {
        BuzzerScan();
        ClrBit(TIM4_CR1, T4IF);
    }
}
/*-------------------------------------------------------------------------------------------------
    Function Name : void CMP0_ISR(void)
    Description   : CMP0/1/2：反电动势检测顺逆风
                                                                                                                                                                                                       CMP0/1/2：顺逆风判断
    Input         : 无
    Output        : 无
-------------------------------------------------------------------------------------------------*/
void CMP_ISR(void) interrupt 7
{
    #if (FRDetectMethod == BEMFMethod)
    //通过BEMF做顺风启动功能
    BEMFDetectFunc();
    #endif
}

/*-------------------------------------------------------------------------------------------------
    Function Name : void UART_ISR(void)
    Description   : UART interrupt，接收数据后再发送收到的数据
    Input         : 无
    Output        : 无
-------------------------------------------------------------------------------------------------*/
void UART_ISR(void) interrupt 14		// UATR 中断号为 14
{
	/****** 如果是接收中断 ******/
    if(ReadBit(UT2_CR , UT2RI))
    {
        ClrBit(UT2_CR , UT2RI);
        UARTData = UT2_DR;			// 接收从PC机发来的数据       
        uart_receive_input(UARTData);
        //put_char(UARTData);	// 将收到的数据发送回PC机验证	     
    }

	/****** 如果是发送中断 ******/
    if(ReadBit(UT2_CR , UT2TI))
    {
        ClrBit(UT2_CR , UT2TI); // 软件清除发送中断标志
    }

}

